package org.grow.stable.ftp;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
public class FtpController {
    @Resource PayBillDao payBillDao;

    @PutMapping("/upload")
    public PayBill insert(@RequestBody PayBill payBill){
        System.out.println("begin to upload bill");
            payBill.setBillCode(null);
            payBill.setLocalDateTime(LocalDateTime.now());
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        System.out.println("auth:"+SecurityContextHolder.getContext().getAuthentication());
//
//        Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();
//        WebAuthenticationDetails d= (WebAuthenticationDetails) details;
//        System.out.println(d.getSessionId());
        payBill.setOperatorId(((UserDetails)credentials).getUsername());
        return payBillDao.save(payBill);
    }
    //lisi zhengqi allowed
    @GetMapping("/download")
    public List<PayBill> queryAll(){
       return payBillDao.findAll();
    }

}
